package casa.depesos.app.com.frag;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import casa.depesos.app.com.activity.WebActivity;
import casa.depesos.app.com.databinding.FragmentTakeCheckBinding;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.RepayBean;
import casa.depesos.app.com.http.bean.ScoreBean;
import casa.depesos.app.com.http.data.Repay;
import casa.depesos.app.com.http.req.IsScoreReq;
import casa.depesos.app.com.http.req.QueryRepayReq;
import casa.depesos.app.com.http.req.no.CanChangeReq;
import casa.depesos.app.com.it.Refreshable;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;


public class TakeCheckFragment extends BannerBaseFragment implements Refreshable {

    protected FragmentTakeCheckBinding binding;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Report.show("ACTION_SS_001");
        new CanChangeReq().req();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = FragmentTakeCheckBinding.inflate(inflater, container, false);
        binding.rela.setPadding(0, Utils.getStatusBarHeight(getActivity()), 0, 0);

        getData();
        score();
        showMsg(binding.msg, binding.scroll);
        getName(binding.top);
        getUnRead(binding.top);

        return binding.getRoot();
    }

    private void getData() {
        showLoading();
        new QueryRepayReq().req(new ReqCallback<RepayBean>() {
            @Override
            public void callback(boolean success, RepayBean bean) {
                hideLoading();
                if (success && bean.isValid()) {
                    show(bean.info);
                }
            }
        });
        //    new CanChangeReq().req();
    }

    private void show(Repay info) {
        binding.amount.setText(Utils.formatNoFloat(info.amount, true));
        binding.day.setText((info.day + 1) + " días");
        binding.card.setText(info.card.cardNo);
        binding.payDay.setText(Utils.getDay3(info.date + ""));
        binding.payAmount.setText(Utils.formatNoFloat(info.repayAmount, true));
    }

    protected void score() {
        new IsScoreReq().req(new ReqCallback<ScoreBean>() {
            @Override
            public void callback(boolean success, ScoreBean bean) {
                if (success && bean.isValid()) {
                    if ("1".equalsIgnoreCase(bean.info.open)) {
                        binding.take.setVisibility(View.VISIBLE);
                        binding.take.setOnClickListener(v -> {
                            Report.click("ACTION_SS_003");
                            WebActivity.startGooglePlay(bean.info.link);
                        });
                    } else {
                        binding.take.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    @Override
    public void refresh() {
        getData();
    }
}
